<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Home_Post extends Controller_Portal {

    public function action_step1() {
        $this->add_css(array(
            'style_1'
        ));
        $this->add_js(array('modernizr.custom.53451', 'jquery.gallery'));
        $this->template->content = View::factory('home/step1');
    }

    public function action_step2() {
        $this->add_js(array('ajaxupload'));
        if ($_POST) {
            $a_post = $this->request->post();
            if (empty($a_post['your_image'])) {
                $filename = uniqid() . ".jpg";
                if (!empty($this->_user['avatar'])) {
                    $request = Request::factory($this->_user['avatar']);
                    $response = $request->execute();
                    $file = 'media/media/upload/temp/' . $filename;
                    $myfile = fopen(DOCROOT . $file, "w");
                    fwrite($myfile, $response);
                    fclose($myfile);
                    $a_post['your_image'] = $filename;
                }
            }
            if (!empty($a_post['tag_image'])) {
                $filename = uniqid() . ".jpg";
                if (!empty($filename)) {
                    $request = Request::factory($a_post['tag_image']);
                    $response = $request->execute();
                    $file = 'media/media/upload/temp/' . $filename;
                    $myfile = fopen(DOCROOT . $file, "w");
                    fwrite($myfile, $response);
                    fclose($myfile);
                    $a_post['tag_image'] = $filename;
                }
            }
            $filetemp = uniqid() . ".jpg";

            $jpg_image = imagecreatefrompng(DOCROOT . 'media/themes/portal/default/img/file-' . Arr::get($_REQUEST, 'id') . '.png');

            // Allocate A Color For The Text
            $white = imagecolorallocate($jpg_image, 228, 0, 123);

            $back = imagecolorallocate($jpg_image, 0, 0, 0);

            // Set Path to Font File
            $font_path = DOCROOT . 'media/themes/portal/default/img/seguili.ttf';
            $font_path2 = DOCROOT . 'media/themes/portal/default/img/seguisbi.ttf';

            // Set Text to Be Printed On Image
            $text = $a_post['comment'];

            // Print Text On Image
            imagettftext($jpg_image, 14, 0, 80, 670, $white, $font_path, $text);
            
            $leng1 = strlen($this->_user['name']);
            $x_text1 = 120 - $leng1;
            imagettftext($jpg_image, 12, 0, $x_text1, 635, $back, $font_path2, $this->_user['name']);
            
            $leng2 = strlen($a_post['tag_name']);
            $x_text2 = 421 - $leng1;
            imagettftext($jpg_image, 12, 0, $x_text2, 635, $back, $font_path2, $a_post['tag_name']);
            // $image = new Image_GD(DOCROOT . 'media/media/upload/temp/' . $a_post['your_image']);
            Image::factory(DOCROOT . 'media/media/upload/temp/' . $a_post['your_image'])
                    ->resize(60, 60, Image::PRECISE)
                    ->save(DOCROOT . 'media/media/upload/temp/' . $a_post['your_image']);

            $a_file1 = explode(".", $a_post['your_image']);
            if(end($a_file1) == 'png'){
                $icon1 = imagecreatefrompng(DOCROOT . 'media/media/upload/temp/' . $a_post['your_image']);
            }else{
                $icon1 = imagecreatefromjpeg(DOCROOT . 'media/media/upload/temp/' . $a_post['your_image']);
            }
            

            imagecopy($jpg_image, $icon1, 120, 555, 0, 0, 60, 60);

            Image::factory(DOCROOT . 'media/media/upload/temp/' . $a_post['tag_image'])
                    ->resize(60, 60, Image::PRECISE)
                    ->save(DOCROOT . 'media/media/upload/temp/' . $a_post['tag_image']);

            $icon2 = imagecreatefromjpeg(DOCROOT . 'media/media/upload/temp/' . $a_post['tag_image']);

            
            imagecopy($jpg_image, $icon2, 421, 555, 0, 0, 60, 60);


            // Send Image to Browser
            imagejpeg($jpg_image, DOCROOT . 'media/media/upload/image/' . $filetemp);

            // Clear Memory
            imagedestroy($jpg_image);
            //save data
            $en_submission = new Domain_Entities_Submission();
            $s_submission = new Model_Service_Submission();
            $en_submission->setUser_id($this->_user['id']);
            $en_submission->setImage($filetemp);
            $en_submission->setTag_id($a_post['tag_id']);
            $en_submission->setTag_name($a_post['tag_name']);
            $en_submission->setTag_comment($a_post['comment']);
            $en_submission->setTag_Image($a_post['tag_image']);
            $en_submission->setCreated(date('Y-m-d H:i:s'));
            $s_submission->insert($en_submission);
            $this->redirect(URL::base()."home/post/step3");
            die;
            
        }
        $this->template->content = View::factory('home/step2');
    }
    
    public function action_step3(){
        $s_submission = new Model_Service_Submission();
        $en_submission = $s_submission->get($this->_user['id'])->get_data();
        
        $this->template->content = View::factory('home/step3')
                ->set("en_submission",$en_submission);
    }

}
